package org.ws.task.starter;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import org.ws.task.TaskMainBootstrap;

@Configuration
@EnableConfigurationProperties({WsTaskProperties.class})
public class DefaultWsTaskConfiguration {

    @Bean
    public WsTaskProperties wsTaskProperties(){
        return new WsTaskProperties();
    }

    @Bean
    public TaskMainBootstrap taskMainBootstrap(WsTaskProperties wsTaskProperties){
        Assert.notNull(wsTaskProperties.getZookeeper(),"ws-task.zookeeper must be not null.");
        Assert.notNull(wsTaskProperties.getNetty(),"ws-task.netty must be not null.");
        Assert.hasText(wsTaskProperties.getZookeeper().getNamespace(),"ws-task.zookeeper.namespace must be not null.");
        Assert.hasText(wsTaskProperties.getZookeeper().getAddress(),"ws-task.zookeeper.address must be not null.");
        Assert.isTrue(wsTaskProperties.getNetty().getServerPort()>0,"ws-task.netty.serverPort must be gt 0.");

        TaskMainBootstrap taskMainBootstrap = new TaskMainBootstrap(wsTaskProperties.getZookeeper(),wsTaskProperties.getNetty());
        taskMainBootstrap.start();
        return taskMainBootstrap;
    }



}
